package com.ruoyi.web.controller.news;

import java.util.ArrayList;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.news.domain.SysNews;
import com.ruoyi.news.service.ISysNewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息提示Controller
 *
 * @author ruoyi
 * @date 2021-12-11
 */
@RestController
@RequestMapping("/system/news")
public class SysNewsController extends BaseController
{
    @Autowired
    private ISysNewsService sysNewsService;

    /**
     * 查询消息提示列表
     */
//    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNews sysNews)
    {
        startPage();
        List<SysNews> list = sysNewsService.selectSysNewsList(sysNews);
        return getDataTable(list);
    }

    /**
     * 导出消息提示列表
     */
//    @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "消息提示", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysNews sysNews)
    {
        List<SysNews> list = sysNewsService.selectSysNewsList(sysNews);
        ExcelUtil<SysNews> util = new ExcelUtil<SysNews>(SysNews.class);
        return util.exportExcel(list, "消息提示数据");
    }

    /**
     * 获取消息提示详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return AjaxResult.success(sysNewsService.selectSysNewsById(newsId));
    }

    /**
     * 新增消息提示
     */
//    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "消息提示", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNews sysNews)
    {
        int row=0;
        String approvalBys[]=sysNews.getApprovalBys();
        if(approvalBys.length>0) {
            for (int i = 0; i < approvalBys.length; i++) {
                sysNews.setApprovalBy(approvalBys[i]);
                row=sysNewsService.insertSysNews(sysNews);
            }
        }
        else
            row= sysNewsService.insertSysNews(sysNews);

        return toAjax(row);
    }

    /**
     * 修改消息提示
     */
//    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "消息提示", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNews sysNews)
    {
        return toAjax(sysNewsService.updateSysNews(sysNews));
    }

    /**
     * 删除消息提示
     */
//    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "消息提示", businessType = BusinessType.DELETE)
	@DeleteMapping
    public AjaxResult remove(@RequestBody SysNews sysNews)
    {
        int row=0;
        row=sysNewsService.deleteSysNews(sysNews);
        return toAjax(1);
    }

    /**
     * 删除一条消息提示
     */
//    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "消息提示", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete1")
    public AjaxResult remove1(@RequestBody SysNews sysNews)
    {

        int row=0;
        String approvalBys[]=sysNews.getApprovalBys();
        try {
            if (approvalBys.length > 0) {
                for (int i = 0; i < approvalBys.length; i++) {
                    sysNews.setApprovalBy(approvalBys[i]);
                    row = sysNewsService.deleteSysNews1(sysNews);
                }
            } else
                row = sysNewsService.deleteSysNews1(sysNews);
        }catch (Exception e){
            e.printStackTrace();
            return toAjax(1);
        }
        return toAjax(1);
    }

}


